package cn.ainit.edi.entity;
/***
 * 解析xml生成建表语句
 */
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import cn.ainit.sys.constant.SystemConstant;
import cn.ainit.sys.constant.TypesConstant;

public class FormatTablesFromFile {
	//文件类型
	private String fileType;
	/***
	 * 建表语句
	 */
	private List<StringBuffer> tableSqls;
	
	/***
	 * 表和字段
	 */
	private Map<String, List<String>> tableColumns = new HashMap<String, List<String>>();
	
	public FormatTablesFromFile(String fileType,File file,String tablePrefix) {
		try {
			if(fileType == null || fileType.equals("")){
				throw new RuntimeException("cn.ainit.edi.entity.FormatTablesFormFile 请传入文件类型");
			}else if(fileType.equals(TypesConstant.EDI_XML_DATA)) {
				this.setTableSqls(formatCreateTable(file,tablePrefix));
			}else if(fileType.equals(TypesConstant.EDI_BIG_BASIC_FIXED)) {
				System.out.println("cn.ainit.edi.entity.FormatTablesFormFile 暂未开通该类型");
			}else if(fileType.equals(TypesConstant.EDI_BIG_BASIC_DELIMITER)){
				System.out.println("cn.ainit.edi.entity.FormatTablesFormFile 暂未开通该类型");
			}else {
				throw new RuntimeException("文件类型不正确");
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	/******************************************************
	 * 根据XML文件，创建缓存表
	 *****************************************************/
	private List<StringBuffer> formatCreateTable(File file,String tablePrefix) throws Exception{
		SAXReader reader = new SAXReader();
		Document document;
		List<StringBuffer> sqlS= new ArrayList<StringBuffer>();
		try {
			document = reader.read(file);
			Element root = document.getRootElement();
			Element koo;
			//sql = "create table test (a_id varchar2(32) primary key,ainit_id varchar2(32), line number(11), column_bak varchar2(4000));"
			for (Iterator i = root.elementIterator(); i.hasNext();) {
				koo = (Element) i.next();
				List<Element> elements = koo.selectNodes("complexType/sequence/element");
				if (!elements.isEmpty()) {
					StringBuffer sql = new StringBuffer("create table ");
					//表
					sql.append(" "+SystemConstant.EDI_TABLE_PREFIX+tablePrefix+koo.attributeValue("name")+" (a_id varchar2(32) primary key,ainit_id varchar2(32),");
					String tableName = koo.attributeValue("name");
					this.tableColumns.put(tableName, new ArrayList<String>());
					for (int k = 0; k < elements.size(); k++) {
						//字段
						sql.append(" "+elements.get(k).attributeValue("ref")+" varchar2(4000),");
						this.tableColumns.get(tableName).add(elements.get(k).attributeValue("ref"));
					}
					sql.append(" line_count number(11), input_date date default sysdate)");
					sqlS.add(sql);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("文件格式不正确");
		}
		return sqlS;
	 }


	public String getFileType() {
		return fileType;
	}

	public void setFileType(String fileType) {
		this.fileType = fileType;
	}

	public List<StringBuffer> getTableSqls() {
		return tableSqls;
	}

	public void setTableSqls(List<StringBuffer> tableSqls) {
		this.tableSqls = tableSqls;
	}

	public Map<String, List<String>> getTableColumns() {
		return tableColumns;
	}

	public void setTableColumns(Map<String, List<String>> tableColumns) {
		this.tableColumns = tableColumns;
	}

	
}
